<template>
  <div v-loading="isLoading > 0" class="page-container">
    <page-tools>
      <template #left>
        <!-- <span>总记录数: {{ total }} 条</span> -->
      </template>
      <template #right>
        <el-button type="primary" size="mini" @click="addCoupon">新增优惠券</el-button>
      </template>
    </page-tools>

    <el-card style="margin-top: 10px;">
      <el-table :data="couponList" border>
        <el-table-column label="序号" type="index" fixed="left" width="60" sortable />
        <el-table-column label="名称" prop="coupon_name" width="100" />
        <el-table-column label="满多少可用" prop="limit_min_money" width="100" />
        <el-table-column label="优惠金额" prop="money" width="100" />
        <el-table-column label="有效时间" prop="term" width="100" />
        <el-table-column label="开始时间" width="110" sortable>
          <template #default="{ row }">{{ row.start_time | formatTime }}</template>
        </el-table-column>
        <el-table-column label="结束时间" width="110" sortable>
          <template #default="{ row }">{{ row.end_time | formatTime }}</template>
        </el-table-column>
        <el-table-column label="是否上线" width="100">
          <template #default="{ row }">
            <el-switch
              :value="row.status === 1"
              active-color="#13ce66"
              inactive-color="#ff4949"
              @change="changeStatus(row)"
            />
          </template>
        </el-table-column>
        <el-table-column label="操作" fixed="right">
          <template #default="{ row }">
            <el-button type="text" size="small" @click="editCoupon(row)">编辑</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <add-edit ref="AddEditDialog" @refresh="getCouponList" />
  </div>
</template>

<script>
import { getCouponCateList, updateOneCouponCateById } from '@/api/coupon'
import AddEdit from './components/AddEdit.vue'
export default {
  name: 'CouponCateList',
  components: {
    AddEdit
  },
  data() {
    return {
      isLoading: 0,
      couponList: []
    }
  },
  computed: {},
  watch: {},
  created() {
    this.getCouponList()
  },
  mounted() {},
  methods: {
    addCoupon() {
      this.$refs.AddEditDialog.open()
    },
    editCoupon(row) {
      this.$refs.AddEditDialog.open(row)
    },
    changeStatus(row) {
      this.$confirm('确定要进行此次操作么？', '温馨提示').then(async() => {
        try {
          this.isLoading++
          await updateOneCouponCateById({
            id: row.id,
            status: row.status === 1 ? -1 : 1
          })

          await this.getCouponList()
        } finally {
          this.isLoading--
        }
      }).catch(() => {
        console.log('取消')
      })
    },
    async getCouponList() {
      try {
        this.isLoading++
        this.couponList = await getCouponCateList()
      } finally {
        this.isLoading--
      }
    }
    // indexMethod(index) {
    //   return index + 1 + (this.page - 1) * this.size
    // },
    // handleSizeChange() {},
    // handleCurrentChange() {}
  }
}
</script>

<style lang="scss" scoped>
.recharge-box {
  list-style: none;
  li {
    display: flex;
    span {
      width: 100px;
    }
  }
}
.img-item {
  height: 80px;
  margin: 0 5px;
}
</style>
